﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace BlackBoard
{
    public class BlackBoard
    {   
        public List<Entry> entries = new List<Entry>();
        public List<string> knowledgeSources = new List<string>();
        private Controller controller;

        public BlackBoard(string configName = "PL")
        {
            var config = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + @"..\..\source\BlackBoard\BlackBoard\config.conf");

            foreach (var temp in config)
            {
                if (configName == GetConfigName(temp)) 
                    knowledgeSources = GetKnowledgeSources(temp);
            }

            if (knowledgeSources.Count == 0)
                throw new ArgumentException("cannot find config with this parameter");

            controller = new Controller(knowledgeSources);
        }

        public void Loop(string input)
        {
            controller.Loop(input);

            foreach (var entry in controller.entries)
            {
                entries.Add(entry);
            }
        }

        public List<String> PrintAll(string input)
        {
            controller.Loop(input);

            List<String> list = new List<string>();
            foreach (var entry in controller.entries)
            {
                list.Add(entry.KnowledgeSourceName + " " + entry.Result+ " " + entry.Time+ " " + entry.Turn);
            }
            return list;
        }

        public List<string> Run(string input)
        {
            controller.Loop(input);
            var entry = controller.entries[controller.entries.Count - 1];
            //return String.Format(entry.KnowledgeSourceName + " " + entry.Result+ " " + entry.Time + " " + entry.Turn);
            return entry.Result;
        }
        public string FirstRun(string input)
        {
            controller.FirstRunLoop(input);
            var entry = controller.entries[controller.entries.Count - 1];
            //return String.Format(entry.KnowledgeSourceName + " " + entry.Result+ " " + entry.Time + " " + entry.Turn);
            return entry.Result[0];
        }
        public void ShowBlackBoard()
        {
            foreach (var entry in entries)
            {
                Console.WriteLine("--------------------");
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", entry.KnowledgeSourceName, entry.Result, entry.Time, entry.Turn);
            }
        }

        public string GetConfigName(string inputText)
        {
            var temp = Regex.Split(inputText, @"\W+");
            return temp[0];    
        }

        public List<string> GetKnowledgeSources(string inputText)
        {
            var temp = Regex.Split(inputText, @"\W+");
            var result = new List<string>();

            for (int i = 1; i < temp.Length-1; i++)
                result.Add(temp[i]);

            return result;
        }
    }
}
